package com.anosine.gpsl.android.ui.fragment;

import com.anosine.gpsl.android.AppConstants;
import com.anosine.gpsl.android.GPSLApp;
import com.anosine.gpsl.android.R;
import com.anosine.gpsl.android.WebConstants;
import com.anosine.gpsl.android.base.BaseFragment;
import com.anosine.gpsl.android.entity.DelHistoryWork;
import com.anosine.gpsl.android.entity.Login;
import com.anosine.gpsl.android.entity.SafeStatus;
import com.anosine.gpsl.android.presenter.BasePresenterFragment;
import com.anosine.gpsl.android.presenter.FortifiedPresenter;
import com.anosine.gpsl.android.presenter.LoginPresenter;
import com.anosine.gpsl.android.ui.FortifiedView;
import com.anosine.gpsl.android.util.DeviceUtil;
import com.anosine.gpsl.android.util.HttpRequestListener;
import com.anosine.gpsl.android.util.SendHttpUtils;
import com.anosine.gpsl.android.util.StringUtil;
import com.anosine.gpsl.android.util.ToastHelper;
import com.anosine.gpsl.android.util.UserInfoProxy;
import com.anosine.gpsl.android.widget.MyWindowManager;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * @Title: TabFortifiedFragment
 * @Package com.anosine.gpsl.android.ui.fragment
 * @Description: 设防界面
 * @author: sunny
 * @date: 2015-11-22
 * @version V1.0
 */

public class TabFortifiedFragment extends BaseFragment implements FortifiedView {
	private myreceiver receiver;
	Login info;
	@ViewInject(R.id.tv_show_result)
	TextView mTvInfo;
	@ViewInject(R.id.FloatWindow)
	TextView FloatWindow;
	@ViewInject(R.id.tv_show_set_result)
	TextView NoJurisdiction;
	@ViewInject(R.id.btn_set_fan)
	Button SetFan;

	// @OnClick(R.id.btn_set_fan)
	// private void btnSetFan(View v) {
	// // 启动MVP
	// initActivityOfSetFan();
	// }

	/**
	 * 方法名：onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
	 * savedInstanceState) 功 能：创建该Fragment的视图 参 数：LayoutInflater inflater
	 * LayoutInflater是用来找res/layout/下的xml布局文件，并且实例化 ViewGroup container
	 * ViewGroup是Layout的基类, Bundle savedInstanceState 返回值： View view
	 */
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_main_tab_fortified,
				container, false);
		ViewUtils.inject(this, v); // 注入view和事件

		FloatWindow.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (MyWindowManager.isShowSetWindow) {
					MyWindowManager.createSmallWindow(getActivity()
							.getApplicationContext());
					MyWindowManager.isShowSetWindow = false;
				} else {
					MyWindowManager.isShowSetWindow = true;
					MyWindowManager.removeSmallWindow(getActivity()
							.getApplicationContext());

				}

			}
		});
		info = UserInfoProxy.getInstance().getLoginInfo();
		getSafestatus();
		boolean isJurisdiction = false;
		if (GPSLApp.mActive.size() > 0) {
			for (int i = 0; i < GPSLApp.mActive.size(); i++) {
				if ((GPSLApp.mActive.get(i).oid).equals(info.SaveOid)) {
					if(info.holdid.equals(GPSLApp.mActive.get(i).hid)){
						isJurisdiction = true;
					}
					break;
				}
			}
		}
		if (isJurisdiction) {
			SetFan.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					initActivityOfSetFan();
				}
			});
		} else {
			NoJurisdiction.setVisibility(View.VISIBLE);
			SetFan.setBackgroundColor(0xffC1BBBB);
			SetFan.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
				}
			});
		}

		receiver = new myreceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction("ChangeFortifiedState");
		getActivity().registerReceiver(receiver, filter);

		return v;
	}

	String GetSafe;

	private void getSafestatus() {
		RequestParams params = new RequestParams();
		if (null != info.SaveOid) {
			params.addBodyParameter("objectid", info.SaveOid);
		} else {
			params.addBodyParameter("objectid", GPSLApp.mActive.get(0).oid);
		}
		SendHttpUtils sendHttp = new SendHttpUtils();
		TypeToken type = new TypeToken<SafeStatus>() {
		};
		String url = String.format("%1s/%2s", AppConstants.sCommonUrl,
				WebConstants.Do_GetSafeStatus_Action);
		sendHttp.doRequest(url, params, type,
				new HttpRequestListener<SafeStatus>() {

					/**
					 * 方法名：onSuccess(String result, final UpdateBean t) 功
					 * 能：json解析成功后进行的操作 参 数：String result　json数据, final
					 * UpdateBean t　解析后的数据
					 * 
					 * @return 无
					 */
					@Override
					public void onSuccess(String result, final SafeStatus t) {
						if (t != null) {
							mTvInfo.setText("0".equals(t.safe) ? "未设防" : "已设防");
							GetSafe = t.safe;
						}
					}

					/**
					 * 方法名：onFailure(String error) 功 能：解析失败或者加载失败后的操作 参 数：String
					 * error　错误信息
					 * 
					 * @return 无
					 */
					@Override
					public void onFailure(String error) {
						ToastHelper.toast(error);
					}
				});
	}

	String infoo;

	private void initActivityOfSetFan() {
		FortifiedPresenter presenter = new FortifiedPresenter(this);
		infoo = "0".equals(GetSafe) ? "1" : "0";
		presenter.loadFortified(infoo, info.objectid, info.userMd5);
	}

	private class myreceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction() == "ChangeFortifiedState") {
				mTvInfo.setText("0".equals(info.safe) ? "未设防" : "已设防");
			} else {
				getActivity().finish();
			}

		}

	}

	@Override
	public void setTabFortified(SafeStatus t) {
		if (StringUtil.isNotEmpty(t.message)
				&& t.message.contains("form_save_success")) {
			mTvInfo.setText("0".equals(infoo) ? "未设防" : "已设防");
		}
	}

}
